flowchart LR
A[package] --> B(module) --> C((function))
style A fill:#DEC62F,stroke:#000000,color:#000000
style B fill:#DE2F99,stroke:#000000,color:#000000
2025-01-21
Motivation
# This script exports metadata information to a JSON file.
# The metadata includes the author, date, and the average resting
# membrane potential, units and the sweep count.
# The output is saved to 'data/data_info.json'.
import json
import numpy as np
path = 'data/'
resting_membrane = [-70.1, -73.3, -69.8, -68.5, -71.2]
resting_membrane_avg = np.mean(resting_membrane)
sweeps = [1, 2, 3, 4, 5]
voltage_unit = 'mV'
sweep_count = len(resting_membrane)
output_file = path + 'data_info.json'
output_data = {
'author': 'Doe, John',
'date': '2025-01-10',
'resting_membrane_avg': resting_membrane_avg,
'unit': voltage_unit,
'sweep_count': sweep_count
}
with open(output_file, 'w') as f:
json.dump(output_data, f)python/python3/py into the consolequit() into the consoleCTRL + SHIFT + P → type: envWindows
Unix/macOS
On your own laptop:
Package managers
Usage
# installing packages from
requirements.txt file
# uninstalling a package
# package with specified version
7.3.2
When do you have to import?
flowchart LR
A[package] --> B(module) --> C((function))
style A fill:#DEC62F,stroke:#000000,color:#000000
style B fill:#DE2F99,stroke:#000000,color:#000000
Let’s open the ‘numpy’ drawer!
This will open our nummpy toolbox drawer
numpy to np)as)Load only parts of the package
import is available. or with fromPhoto by JAQUES London
cell_count will be 1 until changed or deletedKind of like functions1 but different
a operator b+ add two elements together- subtract* multiply** power/ division, // integer1 division% modulusLet’s compare things!
True or False1== equal!= not equal< smaller and > larger<= smaller or equal and >= larger or equalLogical operators check for conditions and returns True or False
and checks if both side are Trueor checks if at least one of the sides is TrueThere are many…
' or " as in 'text' or "text"Split string into multiple strings:
You are curious about other things?
. and press tab twice
output_file.What did you expect?
0
'f', 1: 'o', 2: 'u', 3: 'r''r'
Be careful how you write text or name folders
File "<stdin>", line 1
folder = 'path/sub_path''
^
SyntaxError: unterminated string literal (detected at line 1)
Nonevar_a existsYou can combine single elements into one
strings, ìnt, float, or other objects)( and )tuple but more powerfulLet’s list things!
[ and ]Sometimes you want to access multiple elements in a chain
: to access a slice between to indicesslice(start, stop, step):stop:step or start::stepYou want to to have different things in different things and maybe add some things?
Hello dictionary!
{ and }.get# combine variable types
var_a = 'a variable'
value_a = 42
print('more interesting when we include \n', var_a, 'with value', value_a)
print('the varaible type of var_a is', type(var_a))
print('the varaible type of value_a is', type(value_a))more interesting when we include
a variable with value 42
the varaible type of var_a is <class 'str'>
the varaible type of value_a is <class 'int'>
# sort a list with a method
animal_list = ['SNA 0254581', 'DSC 035576', 'SNA 0954581','SNA 0856662','DSC 024504']
# using the function sorted()
sorted_animal_list = sorted(animal_list)
# same thing but using a method .sort()
animal_list.sort() # sorting in ascending order
print(animal_list)
animal_list.sort(reverse = True) # sorting in descending order
print(animal_list)
num_animals = len(animal_list)
print("I've analyzed the data of", num_animals, "animals.")['DSC 024504', 'DSC 035576', 'SNA 0254581', 'SNA 0856662', 'SNA 0954581']
['SNA 0954581', 'SNA 0856662', 'SNA 0254581', 'DSC 035576', 'DSC 024504']
I've analyzed the data of 5 animals.
import json
import numpy as np
path = 'data/'
resting_membrane = [-70.1, -73.3, -69.8, -68.5, -71.2]
resting_membrane_avg = np.mean(resting_membrane)
sweeps = [1, 2, 3, 4, 5]
voltage_unit = 'mV'
sweep_count = len(resting_membrane)
output_file = path + 'data_info.json'
output_data = {
'author': 'Doe, John',
'date': '2025-01-10',
'resting_membrane_avg': resting_membrane_avg,
'unit': voltage_unit,
'sweep_count': sweep_count
}
with open(output_file, 'w') as f:
json.dump(output_data, f)import json
import numpy as np
def create_meta_data_json(patcher, date_of_rec, RMPs, save_path, save_filename):
avg_RMP = np.mean(RMPs)
num_sweeps = len(RMPs)
output_data = {
'author': patcher,
'date': date_of_rec,
'resting_membrane_avg': avg_RMP,
'unit': 'mV',
'sweep_count': num_sweeps
}
print('saving the file ', save_filename, 'in', save_path)
with open(save_path + save_filename, 'w') as f:
json.dump(output_data, f)
return output_datapatcher = 'Verji'
date_of_rec = '2025-01-15'
save_path = 'data/'
save_fn = 'verji_s_first_recording.json'
resting_membrane = [-70.1, -73.3, -69.8, -68.5, -71.2]
# ways to call the function
create_meta_data_json(patcher = 'Verji', date_of_rec = date_of_rec , RMPs = resting_membrane, \
save_path = save_path, save_filename = save_fn)
out_data = create_meta_data_json(patcher, date_of_rec, resting_membrane, save_path, save_fn)
out_data = create_meta_data_json('Verji', '2025-01-15', [-70.1, -73.3, -69.8, -68.5, -71.2], 'data/', \
'verji_s_first_recording.json')import json
import numpy as np
def create_meta_data_json(patcher, date_of_rec, RMPs, save_path, save_filename):
'''
Returns and saves a dictionary with metadata.
Args: patcher (str): name of experimenter
date_of_rec (str): date of experiment
RMPs (list): list of recorded resting membrane potentials
save_path (str): destination folder
save_filename (str): name of the file
Returns: output_data (dictionary): containing the metadata
'''
avg_RMP = np.mean(RMPs)
num_sweeps = len(RMPs) # have as many sweeps as values
# define metadata dictionary
output_data = {
'author': patcher,
'date': date_of_rec,
'resting_membrane_avg': avg_RMP,
'unit': 'mV', # always mV for RMP
'sweep_count': num_sweeps
}
# confirmation to user
print('saving the file ', save_filename, 'in', save_path)
with open(save_path + save_filename, 'w') as f:
json.dump(output_data, f)
return output_data
resting_membrane = [-70.1, -73.3, -69.8, -68.5, -71.2]
# create a out_dict containing the metadata
out_dict = create_meta_data_json('Verji', '2025-01-15', resting_membrane, 'data/', 'verji_s_first_recording.json')